package com.seeyon.apps.kkSample.utils;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import com.seeyon.ctp.organization.bo.*;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.DateUtil;
import com.seeyon.ctp.util.Strings;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CrsccenterKJchangeUtil {

    // 枚举
    public static String enumChange(String s) throws Exception {
        if (Strings.isBlank(s))
            return "";
        Long l = Long.parseLong(s);
        EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumItem eunmben2 = enumManager.getCtpEnumItem(l);
        return eunmben2.getShowvalue();

    }
    // 枚举编码截取
    public static String enumCodeChange(String s) throws Exception {
        if (Strings.isBlank(s))
            return "";
        Long l = Long.parseLong(s);
        EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
        CtpEnumItem eunmben2 = enumManager.getCtpEnumItem(l);
        if(eunmben2.getEnumItemCode()!=null&&eunmben2.getEnumItemCode().contains("-")){
            return eunmben2.getEnumItemCode().substring(0,eunmben2.getEnumItemCode().indexOf("-"));
        }
        return eunmben2.getEnumItemCode();

    }
    //枚举多选
    public static String enumsCodeChange(String s) throws Exception {
        if (Strings.isBlank(s))
            return "";
        String[] enum_ids=s.split(",");
        StringBuffer sb=new StringBuffer();
        for (String enumid: enum_ids) {
            Long l = Long.parseLong(enumid);
            EnumManager enumManager = (EnumManager) AppContext.getBean("enumManagerNew");
            CtpEnumItem eunmben2 = enumManager.getCtpEnumItem(l);
            if(eunmben2.getEnumItemCode().contains("-")){
                sb.append(eunmben2.getEnumItemCode(), 0, eunmben2.getEnumItemCode().indexOf("-"));
                sb.append(",");
            }
        }

        return sb.toString().substring(0,sb.toString().length()-1);

    }
    //long ref_enumid=formBean.getFieldBeanByDisplay("合同状态").getEnumId();//字段绑定的枚举id
    /**
     *
     * @param enumName 枚举项显示名称
     * @param ref_enumid  字段绑定枚举唯一id
     * @return
     * @throws Exception
     */
    public static Long getEnumIdByName(String enumName,long ref_enumid) throws Exception {
        EnumManager enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
        List<CtpEnumItem> list = enumManagerNew.getEnumItemByName(enumName,ref_enumid,null,null,null);
        for (CtpEnumItem item : list) {
            if (ref_enumid==item.getRefEnumid()) {
                return item.getId();
            }
        }
        return null;
    }


    // 日期
    public static String dataChangeFor(String s,String type) throws Exception {
        if (Strings.isBlank(s))
            return "";
        String st = s.toString();
        Date datea = DateUtil.parse(st, "EEE MMM dd HH:mm:ss zzz yyyy");
        SimpleDateFormat format1 = new SimpleDateFormat(type);
        return format1.format(datea);
    }
    // 日期
    public static String dataChange(String s) throws Exception {
        if (Strings.isBlank(s))
            return "";
        String st = s.toString();
        Date datea = DateUtil.parse(st, "EEE MMM dd HH:mm:ss zzz yyyy");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(datea);
    }

    // 日期时间
    public static String dataTimeChange(String s) throws Exception {
        if (Strings.isBlank(s))
            return "";
        String st = s.toString();
        Date datea = DateUtil.parse(st, "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        return format1.format(datea);
    }

    // 成员
    public static String getMemberNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgMember member = orgManager.getMemberById(Long.parseLong(id));
        return  member == null ? "" :member.getName();
    }
    // 成员
    public static String getMemberCodeById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgMember member = orgManager.getMemberById(Long.parseLong(id));
        return  member == null ? "" :member.getCode();
    }

    //选多人控件获取多人邮件地址

    public static String getMembersLoginNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        String[] sourceStrArray = id.split(",");//分割出来的字符数组
        String ids="";
        for (int i = 0; i < sourceStrArray.length; i++) {
            V3xOrgMember member = orgManager.getMemberById(Long.parseLong(sourceStrArray[i]));
            id = member.getLoginName();
            ids=id+","+ids;
        }
        return Strings.isNotBlank(ids)?ids.substring(0,ids.length()-1):"";

    }

    // 部门
    public static String getDepNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgDepartment dep = orgManager.getDepartmentById(Long.parseLong(id));
        return dep == null ? "" : dep.getName();

    }
    // 部门
    public static String getDepCodeById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgDepartment dep = orgManager.getDepartmentById(Long.parseLong(id));
        return dep == null ? "" : dep.getCode();

    }
    // 单位
    public static String getAccountNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgAccount acc = orgManager.getAccountById(Long.parseLong(id));
        return acc == null ? "" : acc.getName();

    }
    // 单位
    public static String getAccountCodeById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgAccount acc = orgManager.getAccountById(Long.parseLong(id));
        return acc == null ? "" : acc.getCode();

    }

    // 岗位
    public static String getPostNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgPost post = orgManager.getPostById(Long.parseLong(id));
        return post == null ? "" : post.getName();

    }

    // 职级
    public static String getLevelNameById(String id) throws Exception {
        if (Strings.isBlank(id))
            return "";
        OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
        V3xOrgLevel lev = orgManager.getLevelById(Long.parseLong(id));
        return lev == null ? "" : lev.getName();

    }
}
